﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Linq.Expressions;
using Microsoft.AspNetCore.Authorization;
using Machine.Bussiness;

namespace Machine.Controllers
{
    /// <summary>
    /// Machine controller.
    /// </summary>
    [Route("api/[controller]")]
    [Authorize(Roles = "Admin")]
    public class MachineController : Controller
    {
        private MachineBLL _MachineBLL = new MachineBLL();

        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <returns>The machine page list.</returns>
        /// <param name="para"></param>
        [HttpPost]
        [Route("machine")]
        public JsonResult GetMachinePageList([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            int pageIndex = 1, pageSize = 10;
            string key = para["key"].ToString();
            pageIndex = Convert.ToInt32(para["pageIndex"]);
            pageSize = Convert.ToInt32(para["pageSize"]);
            Expression<Func<Machine.Entity.Machine, bool>> where = (p => p.Status != "STOP" &&
                                                                        p.MacID.StartsWith("M") &&
                                                                        (p.MacID.Contains(key)
                                                                        || p.MacName.Contains(key)
                                                                        || p.ModelNo.Contains(key)));
            return Json(_MachineBLL.GetPageList(where, pageIndex, pageSize));
        }

        /// <summary>
        /// 根据macid查询设备
        /// </summary>
        /// <returns>设备信息</returns>
        /// <param name="para"></param>
        [HttpPost]
        [Route("getInfo")]
        public JsonResult GetMachineByMacid([FromBody]Newtonsoft.Json.Linq.JObject para)
        {
            string macid = para["macid"].ToString();
            Expression<Func<Machine.Entity.Machine, bool>> where = (p => p.Status != "STOP" && (p.MacID == macid));
            Machine.Entity.Machine Entity = _MachineBLL.GetByWhere(where);
            if(Entity != null){
                return Json(new { recode = 0, info = Entity });
            }
            return Json(new { recode = 0, msg = "没有找到资料" });
        }
    }
}